/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.android.apps.mytracks.services.sensors.ant;

import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.content.Sensor.SensorDataSet;

import android.content.Context;
import android.content.SharedPreferences;
import android.test.AndroidTestCase;

/**
 * Tests {@link AbstractAntSensorHandler}
 *
 * @author Matthew Simmons
 */
public class AbstractAntSensorHandlerTest extends AndroidTestCase {
  private static final AntSensor SENSOR = AntSensor.SENSOR_HEART_RATE;

  private static class TestAntSensorHandler extends AbstractAntSensorHandler {
    public TestAntSensorHandler(Context context, SharedPreferences prefs) {
      super(context, prefs);
    }

    @Override
    public AntSensor getSensorType() {
      return SENSOR;
    }

    @Override
    public SensorDataSet handleBroadcastData(byte[] antMessage) {
      return SensorDataSet.newBuilder()
          .setCreationTime(0x1234)
          .build();
    }
  }

  private SharedPreferences prefs;
  private String prefKey;

  @Override
  public void setUp() throws Exception {
    super.setUp();

    prefs = getContext().getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
    prefs.edit().clear().commit();

    prefKey = getContext().getString(SENSOR.getPrefKey());
  }

  public void testGetChannelNumber() {
    AntSensorHandler handler = new TestAntSensorHandler(getContext(), prefs);
    assertEquals(handler.getSensorType().getChannelNumber(), handler.getChannelNumber());
  }

  public void testGetDeviceNumber() {
    prefs.edit().putInt(prefKey, 0x1234).commit();
    AntSensorHandler handler = new TestAntSensorHandler(getContext(), prefs);
    assertEquals(0x1234, handler.getDeviceNumber());
  }

  public void testSetDeviceNumber() {
    prefs.edit().putInt(prefKey, 0x1234).commit();
    AntSensorHandler handler = new TestAntSensorHandler(getContext(), prefs);
    handler.setDeviceNumber((short) 0x4321);

    assertEquals(0x4321, handler.getDeviceNumber());
    assertEquals(0x4321, prefs.getInt(prefKey, 0));
  }
}
